
'''
https://leetcode.cn/problems/valid-anagram/description/
https://programmercarl.com/0242.%E6%9C%89%E6%95%88%E7%9A%84%E5%AD%97%E6%AF%8D%E5%BC%82%E4%BD%8D%E8%AF%8D.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE
有效的字母异位词
a = "cat" , b = "tca"
a,b是异位词，返回true
a = "cat" , b = "ttt" 返回false
'''


def l242(s: str, t: str) -> bool:
    dict_s = {}
    for i in s:
        if dict_s.get(i):
            dict_s[i] += 1
        else:dict_s[i] = 1
    for i in t:
        if dict_s.get(i):
            dict_s[i] -= 1
        else:return False
    for i in s:
        if dict_s.get(i) != 0:
            return False
    return True


'''
from collections import defaultdict
defaultdict 是 Python 标准库 collections 模块中的一个类，它继承自内置的 dict 类型，并提供了带有默认值的字典功能。
这意味着当你尝试访问一个不存在的键时，defaultdict 会自动为这个键赋予一个默认值，而不是抛出 KeyError。
'''
def l242_1(s: str, t: str) -> bool:
    from collections import defaultdict
    dict_s = defaultdict(int) #设置不存在的key赋初值int类型0
    dict_t = defaultdict(int) #设置不存在的key赋初值int类型0
    for i in s:
        dict_s[i] += 1
    for i in t:
        dict_t[i] += 1
    return dict_t == dict_s

def l242_2(s: str, t: str) -> bool:
    from collections import Counter
    s_count = Counter(s)
    t_count = Counter(t)
    return s_count == t_count
    pass
if __name__ == '__main__':
    print(l242("tcatrw","tctar"))
    pass